package code.juc.stopThread;

/**
 * 第三种停止线程的方法： 当线程任务中, while循环中存在sleep这类阻塞方法时
 *
 * 1. 不需要再使用isInterrupted()方法的判断.
 * 2. 当使用interrupt()方法中断线程时, 就能及时中断线程.
 */
public class RightWayStopThreadWithSleepEveryLoop {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            try {
                while (num < 10_000) {
                    if (num % 100 == 0) {
                        System.out.println(num + "is mod 100!");
                    }

                    num++;
                    Thread.sleep(10);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5_000);
        thread.interrupt();
    }
}
